import java.io.BufferedWriter;
import java.util.*;
import java.io.*;

//OUTDATED FILE, NO LONGER USED
public class ICS {
  public static void main(String args[]) {
        
    //Prompt user on the aspects of their event
    
    /*Requirements:
    Version     (section        3.7.4   of RFC  5545)   
    Classification      (3.8.1.3).
    Geographic  Position (3.8.1.6)
    Priority (3.8.1.9)
    Summary (3.8.1.12)  
    DTSTART     (3.8.2.4)       
    DTEND       (3.8.2.2)       
    Time        zone    identifier      (3.8.3.1) */
    
    String begin = "BEGIN:VCALENDAR";
    String calscale = "CALSCALE:GREGORIAN";
    String method = "METHOD:PUBLISH";
    String prodid = "PRODID:-//MEKONG//MEKONGCALENDAR v1.0//EN";
    String version = "VERSION:2.0";
    String classification = "";
    String geoPosition = "GEO:37.386013;-122.082932";
    String summary ="";
    String DTStart = "";
    String DTEnd ="";
    String timeZoneIdt ="TZID:America/New_York";
    String UID = "";
    boolean resume = false;

    int priority = 0;
    int month = 0;
    int day = 0;
    int year = 0;
    int time = 0;
  
    Scanner input = new Scanner(System.in);
    
    BufferedWriter eventWriter = null;
    //Creates ics file
    try {
     eventWriter = new BufferedWriter(new FileWriter(new File("event.ics")));
    }
    catch (IOException e) {
      System.out.println("Error"); 
    }
    
    //Write the basic elements to the file
    write(eventWriter, begin);
    write(eventWriter, version);
    write(eventWriter, prodid);
    write(eventWriter, "BEGIN:VEVENT");
    
    
    //While we are still waiting for a (proper) input
    //For start date
    while(!resume){
      //Get inputs, must be ints separated by commas
      System.out.print("Enter the start month (MM): ");
      if(input.hasNextInt()){
        month = input.nextInt();
      }
      else{
        resume = false;
      }
      System.out.print("Enter the start day (DD): ");
      if(input.hasNextInt()){
        day = input.nextInt();
      }
      else{
        resume = false;
      }
      System.out.print("Enter the start year (YYYY): ");
      if(input.hasNextInt()){
        year = input.nextInt();
      }
      else{
        resume = false;
      }
      
      System.out.print("Enter the start time in 24 hour time (TTTT): ");
      if(input.hasNextInt()){
        time = input.nextInt();
        resume = true;
      }
      else{
        resume = false;
      }
      
      //If it is an invalid date, retry.  Have to modify this to take into account days in each specific month in a specific year
      if(!(month <= 12 && month > 0 && day <= 31 && day > 0 && year < 10000 && year > 0 && time >= 0 && time < 2400)){
        resume = false;
      }
      
      //If invalid entry in any way, retry
      if(!resume){
        System.out.println("INVALID FORMAT!");
      }
    }
    
    resume = false;
    String datestart = "DTSTART:";
    String s_month;
    String s_day;
    String s_year;
    String s_time;
    if(month < 10){
      s_month = "0" + month;
    }
    else{
      s_month = Integer.toString(month);
    }
    
    if(day < 10){
      s_day = "0" + day;
    }  
    else{
      s_day = Integer.toString(day);
    }
    if(year < 10){
      s_year = "000" + year;
    }
    else if(year < 100){
      s_year = "00" + year;
    }
    else if(year < 1000){
      s_year = "0" + year;
    }
    else{
      s_year = Integer.toString(year);
    }
    if(time < 10){
      s_time = "000" + time;
    }
    else if(time < 100){
      s_time = "00" + time;
    }
    else if(time < 1000){
      s_time = "0" + time;
    }
    else{
      s_time = Integer.toString(time);
    }
    
    s_time = s_time + "00";
    
    datestart = datestart + s_year + s_month + s_day + "T" + s_time;
    write(eventWriter, datestart);
    
    
    //End date
    //While we are still waiting for a (proper) input
    
    while(!resume){
      
      //Get inputs, must be ints separated by commas
      System.out.print("Enter the end month: ");
      if(input.hasNextInt()){
        month = input.nextInt();
      }
      else{
        resume = false;
      }
      System.out.print("Enter the end day: ");
      if(input.hasNextInt()){
        day = input.nextInt();
      }
      else{
        resume = false;
      }
      System.out.print("Enter the end year: ");
      if(input.hasNextInt()){
        year = input.nextInt();
      }
      else{
        resume = false;
      }
      System.out.print("Enter the end time: ");
      if(input.hasNextInt()){
        time = input.nextInt();
        resume = true;
      }
      else{
        resume = false;
      }
      
      //If it is an invalid date, retry.  Have to modify this to take into account days in each specific month in a specific year
      if(!(month <= 12 && month > 0 && day <= 31 && day > 0 && year < 10000 && year > 0 && time >= 0 && time < 2400)){
        resume = false;
      }
      
      //If invalid entry in any way, retry
      if(!resume){
        System.out.println("INVALID FORMAT!");
      }
    }
    String dateend = "DTEND:";
    if(month < 10){
      s_month = "0" + month;
    }
    else{
      s_month = Integer.toString(month);
    }
    
    if(day < 10){
      s_day = "0" + Integer.toString(day);
    }  
    else{
      s_day = Integer.toString(day);
    }
    if(year < 10){
      s_year = "000" + Integer.toString(year);
    }
    else if(year < 100){
      s_year = "00" + Integer.toString(year);
    }
    else if(year < 1000){
      s_year = "0" + Integer.toString(year);
    }
    else{
      s_year = Integer.toString(year);
    }
    if(time < 10){
      s_time = "000" + Integer.toString(time);
    }
    else if(time < 100){
      s_time = "00" + Integer.toString(time);
    }
    else if(time < 1000){
      s_time = "0" + Integer.toString(time);
    }
    else{
      s_time = Integer.toString(time);
    }
    
    s_time = s_time + "00";
    
    dateend = dateend + s_year + s_month + s_day + "T" + s_time;
    
    write(eventWriter, dateend);
    
    String description = "";
    description = input.nextLine();
    System.out.print("Enter a description for the event: ");
    description = input.nextLine();
    summary = "SUMMARY:" + description;
    write(eventWriter, summary);
    System.out.println("description is : " + description);

    //get the location
    String location = "LOCATION:";
    System.out.print("Enter the location of the event: ");
    location += input.nextLine();

    write(eventWriter, location);
      
    //finish the file
    write(eventWriter, "END:VEVENT");
    write(eventWriter, "END:VCALENDAR");
    try {
      eventWriter.close();
    }
    catch (IOException e) {
      System.out.println("Error in closing file"); 
    }

 }// end main

  //Writes a line of code to the file.  Take a buffered writer and a string and returns nothing.
  public static void write(BufferedWriter eventWriter, String string){
    try{
      eventWriter.write(string, 0, string.length());
      eventWriter.newLine();
    }
    catch (IOException e){
      System.err.println("Caught IOException: " + e.getMessage());
    }
  }
}
